/* Copyright (c) 2011 by Mani Brundha Kattamuri, Haritha Subramanyam
 *
 * Project Info:  http://code.google.com/p/andriod-travelapp-csc780/wiki/Design
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */
package com.example.TravelApp.search.advanced;

import com.example.TravelApp.R;
import com.example.TravelApp.constants.AdvancedLocationInfo;
import com.example.TravelApp.search.common.GPlaceObjectActivity;
import com.example.TravelApp.search.common.helper.GeoCoderForLocationHelper;

import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.Spinner;
import android.content.Intent;
import android.util.SparseBooleanArray;
import android.view.View;

/**
 * This activity is called when the user clicks on the advanced tab. The layout
 * is described in file <code>res/layout/main.xml</code>. This activity is user
 * to collect the user preferences of time, interest and place.
 * 
 * @author Haritha Subramanyam, Mani Brundha
 * 
 */
public class AdvancedActivity extends Activity {

	private ListView lView;
	private SparseBooleanArray sparseBoolArr;
	private String place, time, intrest, HourAndDays;

	/**
	 * Called when the activity is first created.
	 */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		intrest = "";
		// drop-down list for time and place
		Spinner spinnerPlaces = (Spinner) findViewById(R.id.spinnerPlaces);
		Spinner spinnerHrsDays = (Spinner) findViewById(R.id.spinnerHrsDays);
		Spinner spinnerTime = (Spinner) findViewById(R.id.spinnerTime);

		/*
		 * Create adapters for place, time and interest and set these adapters
		 * to the spinners and these adapters listen to the user clicks
		 */
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.places_array,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerPlaces.setAdapter(adapter);
		spinnerPlaces.setOnItemSelectedListener(new OnPlaceSelectedListener());

		ArrayAdapter<CharSequence> adapterHrsDays = ArrayAdapter
				.createFromResource(this, R.array.hoursOrDays_array,
						android.R.layout.simple_spinner_item);
		adapterHrsDays
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerHrsDays.setAdapter(adapterHrsDays);
		spinnerHrsDays
				.setOnItemSelectedListener(new MyOnItemSelectedHourAndDaysListener());

		ArrayAdapter<CharSequence> adapterTime = ArrayAdapter
				.createFromResource(this, R.array.times_array,
						android.R.layout.simple_spinner_item);
		adapterTime
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spinnerTime.setAdapter(adapterTime);
		spinnerTime.setOnItemSelectedListener(new OnTimeSelectedListener());

		lView = (ListView) findViewById(R.id.ListView01);
		// Set option as Multiple Choice. So that user can able to select more
		// the one option from list
		intrest = "";
		lView.clearChoices();
		lView.setAdapter(new ArrayAdapter<String>(this,
				R.layout.simple_list_item_multiple_choice1,
				AdvancedLocationInfo.getLVItemsAdvanced()));
		lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		lView.setOnItemSelectedListener(new OnInterestSelectedListener());

	}

	/**
	 * Called whenever AdvancedOptionOverviewActivity terminates.
	 */

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		intrest = "";
		lView.clearChoices();
		super.onActivityResult(requestCode, resultCode, data);

	}

	public void onResume() {
		intrest = "";
		lView.clearChoices();
		super.onResume();
	}

	/**
	 * Implements an action listener for collecting place from user Listens to
	 * the user's click and captures the place
	 */
	public class OnPlaceSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {

			// Retrieves the location coordinates of the location selected from
			// the spinner
			place = GeoCoderForLocationHelper
					.getLocationCoordinates(AdvancedLocationInfo
							.getPlacesArray()[pos]);
		}

		public void onNothingSelected(AdapterView<?> parent) {
			// Do nothing.
		}
	}

	/**
	 * Implements an action listener for collecting interests from user Listens
	 * to the user's click and captures the interest
	 */
	public class OnInterestSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			intrest = parent.getItemAtPosition(pos).toString();

		}

		public void onNothingSelected(AdapterView<?> parent) {
			intrest = null;
		}
	}

	/**
	 * Implements an action listener for collecting time from user Listens to
	 * the user's click and captures the time
	 */
	public class OnTimeSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			time = parent.getItemAtPosition(pos).toString();
		}

		public void onNothingSelected(AdapterView parent) {
			// Do nothing.
		}
	}

	/**
	 * Implements an action listener for collecting whether the user specified
	 * hours or days
	 */
	public class MyOnItemSelectedHourAndDaysListener implements
			OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			HourAndDays = parent.getItemAtPosition(pos).toString();

		}

		public void onNothingSelected(AdapterView<?> parent) {
			// Do nothing.
		}
	}

	/**
	 * Invoked whenever the button is clicked in the AdvancedActivity. Triggers
	 * the invocation of AdvancedOptionOverviewActivity and sends in the users
	 * selected preferences as intent.
	 */
	public void onClick(View v) {
		sparseBoolArr = new SparseBooleanArray();
		sparseBoolArr.clear();
		intrest = "";
		sparseBoolArr = lView.getCheckedItemPositions();

		for (int i = 0; i < sparseBoolArr.size(); i++) {
			if (i == 0)
				intrest = AdvancedLocationInfo.getLVItemsAdvanced()[sparseBoolArr
						.keyAt(i)];
			else
				intrest = intrest
						+ "%7C"
						+ AdvancedLocationInfo.getLVItemsAdvanced()[sparseBoolArr
								.keyAt(i)];
		}
		Intent intent = new Intent(this, AdvancedOptionOverviewActivity.class)
				.putExtra("Place", place).putExtra("Intrest", intrest)
				.putExtra("Time", time);
		startActivityForResult(intent, 0);
	}

}